//
//  WZsenderMoneyViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/7/26.
//  Copyright © 2016年 wanzhao. All rights reserved.
//
#define FontSize 12
#import "WZsenderMoneyViewController.h"
#import "Masonry.h"
#import "UINavigationItem+CustomBarButtonItem.h"
#import "EFengComposeTextView.h"
#import "UIUtil.h"
#import "MBProgressHUD+WZ.h"
#import "JKAlertDialog.h"
#import "CGroupModel.h"
#import "CXmppRequestMgr.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "PopPasswordView.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface WZsenderMoneyViewController ()<UITextViewDelegate,EFengComposeTextViewDelegate,PopPasswordViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIView *balanceView;
@property (nonatomic,weak) UILabel *balanceLable;
@property (nonatomic,strong) UIView *moneyView;
@property (nonatomic,strong) EFengComposeTextView *contentview;
@property (nonatomic,weak) UITextField *money;
@property (nonatomic,strong) UIButton *sendButton;
@property (nonatomic,strong) UILabel *resultLabel;
@property (nonatomic,strong) UIView *countView;
@property (nonatomic,weak) UITextField *count;
@property (nonatomic) moneyType type;
@property (nonatomic,weak) UILabel *noticeLabel;
@property (nonatomic,strong) UIScrollView *backScrollView;
@property (nonatomic,strong)CContactModel *currentUser;
@property(nonatomic,retain)PopPasswordView *passwordView;
@end

@implementation WZsenderMoneyViewController
-(CContactModel *)currentUser
{
    if (_currentUser == nil) {
        _currentUser = [[[CContactMgr alloc]init] loadCurContact];
    }
    return _currentUser;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(243, 243, 243);
    [self.navigationItem setNewTitleCenter:@"发红包"];
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(214, 79, 71)];
    }
    else
    {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(214, 79, 71)];
    }
    self.backScrollView = [[UIScrollView alloc]init];
    self.backScrollView.frame = self.view.bounds;
    _backScrollView.scrollEnabled = NO;
    [_backScrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2)];
    [self.view addSubview:self.backScrollView];
    self.type = PINSHOUQI;
    [self setBalanceView];
    [self loadNotification];
   
}
-(void)loadNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginkeyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
-(void)setBalanceView
{
    __weak typeof(self) weakself = self;
    self.balanceView = [[UIView alloc]init];
    [self.backScrollView addSubview:self.balanceView];
    [self.balanceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(20);
        make.centerX.mas_equalTo(weakself.view);
    }];
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor grayColor];
    label.text = @"您钱包当前余额:";
    label.textAlignment = NSTextAlignmentRight;
    label.font = [UIFont systemFontOfSize:FontSize];
    [self.balanceView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakself.balanceView.mas_left).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(0);
    }];
    UILabel *unitLable = [[UILabel alloc]init];
    unitLable.text = @"元";
    unitLable.font = [UIFont systemFontOfSize:FontSize];
    unitLable.textColor = [UIColor grayColor];
    [self.balanceView addSubview:unitLable];
    [unitLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(30);
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    UILabel *balanceLable = [[UILabel alloc]init];
    balanceLable.text = @"----";
    balanceLable.textAlignment = NSTextAlignmentCenter;
    balanceLable.font = [UIFont boldSystemFontOfSize:15.0];
    balanceLable.textColor = [UIColor blackColor];
    [self.balanceView addSubview:balanceLable];
    [balanceLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(0);
        make.right.equalTo(unitLable.mas_left).offset(0);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(0);
    }];
    self.balanceLable = balanceLable;
}
-(void)setupPersonMoneyView
{
    __weak typeof(self) weakself = self;
    self.moneyView = [[UIView alloc]init];
    self.moneyView.backgroundColor = [UIColor whiteColor];
    _moneyView.layer.borderWidth = 0.5;
    _moneyView.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _moneyView.layer.cornerRadius = 2.5f;
    [self.backScrollView addSubview:self.moneyView];
    [self.moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.balanceView.mas_bottom).offset(20);
        make.left.equalTo(weakself.backScrollView.mas_left).offset(20);
        make.width.mas_equalTo(weakself.view.width-40);
        make.height.mas_equalTo(40);
    }];
    UILabel *label1 = [[UILabel alloc]init];
    label1.textAlignment = NSTextAlignmentLeft;
    label1.text = @"金额";
    label1.font = [UIFont systemFontOfSize:FontSize];
    [self.moneyView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(0);
    }];
    UILabel *label2 = [[UILabel alloc]init];
    label2.textAlignment = NSTextAlignmentRight;
    label2.text = @"元";
    label2.font = [UIFont systemFontOfSize:FontSize];
    [self.moneyView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.height.mas_equalTo(40);
        make.top.mas_equalTo(0);
    }];
    UITextField *moneytextField = [[UITextField alloc]init];
    moneytextField.textAlignment = NSTextAlignmentRight;
    moneytextField.placeholder = @"请填写金额";
    moneytextField.font = [UIFont systemFontOfSize:FontSize];
    moneytextField.keyboardType = UIKeyboardTypeDecimalPad;
    moneytextField.delegate = self;
    [moneytextField addTarget:self action:@selector(textchange:) forControlEvents:UIControlEventEditingChanged];
    [self.moneyView addSubview:moneytextField];
    self.money = moneytextField;
    [moneytextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label1.mas_right).offset(0);
        make.right.equalTo(label2.mas_left).offset(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    self.contentview = [[EFengComposeTextView alloc]init];
    _contentview.layer.borderWidth = 0.5f;
    _contentview.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _contentview.layer.cornerRadius = 2.5f;
    _contentview.eFengComposeTextViewDelegate = self;
    _contentview.textFont = [UIFont systemFontOfSize:FontSize];
    [_contentview setMAXCHARACTERCOUNT:30];
    [_contentview setPlaceHoldText:@"送你一个红包"];
    _contentview.backgroundColor = WhiteColor;
    _contentview.userInteractionEnabled = YES;
    [self.backScrollView addSubview:_contentview];
    [_contentview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.moneyView.mas_bottom).offset(20);
        make.height.mas_equalTo(60);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(weakself.view.width-40);
    }];
    self.resultLabel = [[UILabel alloc]init];
    self.resultLabel.text = @"¥0.00";
    self.resultLabel.textAlignment = NSTextAlignmentCenter;
    self.resultLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [self.backScrollView addSubview:_resultLabel];
    [_resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentview.mas_bottom).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
        make.centerX.mas_equalTo(weakself.view);
    }];
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendButton addTarget:self action:@selector(sendMoney) forControlEvents:UIControlEventTouchUpInside];
    [self.sendButton setBackgroundImage:[UIImage imageNamed:@"r-send-button-moren"] forState:UIControlStateDisabled];
    [self.sendButton setBackgroundImage:[UIImage imageNamed:@"r-send-button-click"] forState:UIControlStateNormal];
    [self.sendButton setTitle:@"发红包" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:FontSize];
    self.sendButton.enabled = NO;
    self.sendButton.layer.cornerRadius = 5;
    self.sendButton.layer.masksToBounds = YES;
    [self.backScrollView addSubview:self.sendButton];
    [_sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.resultLabel.mas_bottom).offset(20);
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.width.mas_equalTo(weakself.view.width-80);
    }];
}
-(void)setupGroupMoneyView
{
    self.type = PINSHOUQI;
    __weak typeof(self) weakself = self;
    self.countView = [[UIView alloc]init];
    _countView.backgroundColor = [UIColor whiteColor];
    _countView.layer.borderWidth = 0.5;
    _countView.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _countView.layer.cornerRadius = 2.5f;
    [self.backScrollView addSubview:self.countView];
    [_countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.balanceView.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(weakself.view.width - 40);
        make.height.mas_equalTo(40);
    }];
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentLeft;
    label.text = @"个数";
    label.font = [UIFont systemFontOfSize:FontSize];
    [self.countView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(0);
    }];

    UILabel *countLabel = [[UILabel alloc]init];
    countLabel.textAlignment = NSTextAlignmentRight;
    countLabel.font = [UIFont systemFontOfSize:FontSize];
    countLabel.text = @"个";
    [self.countView addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.height.mas_equalTo(40);
        make.top.mas_equalTo(0);
    }];
    UITextField *count = [[UITextField alloc]init];
    count.placeholder = @"请填写个数";
    count.textAlignment = NSTextAlignmentRight;
    count.keyboardType = UIKeyboardTypeNumberPad;
    count.font = [UIFont systemFontOfSize:FontSize];
    [count addTarget:self action:@selector(checkNumber:) forControlEvents:UIControlEventEditingChanged];
    [self.countView addSubview:count];
    self.count = count;
    [count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(countLabel.mas_left).offset(0);
        make.width.mas_equalTo(100);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    self.moneyView = [[UIView alloc]init];
    self.moneyView.backgroundColor = [UIColor whiteColor];
    _moneyView.layer.borderWidth = 0.5;
    _moneyView.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _moneyView.layer.cornerRadius = 2.5f;
    [self.backScrollView addSubview:self.moneyView];
    [self.moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.countView.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(weakself.view.width - 40);
        make.height.mas_equalTo(40);
    }];
    UILabel *label1 = [[UILabel alloc]init];
    label1.textAlignment = NSTextAlignmentLeft;
    label1.text = @"金额";
    label1.font = [UIFont systemFontOfSize:FontSize];
    [self.moneyView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(0);
    }];
    UILabel *label2 = [[UILabel alloc]init];
    label2.textAlignment = NSTextAlignmentRight;
    label2.text = @"元";
    label2.font = [UIFont systemFontOfSize:FontSize];
    [self.moneyView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.height.mas_equalTo(40);
        make.top.mas_equalTo(0);
    }];
    UITextField *moneytextField = [[UITextField alloc]init];
    moneytextField.textAlignment = NSTextAlignmentRight;
    moneytextField.placeholder = @"请填写金额";
    moneytextField.font = [UIFont systemFontOfSize:FontSize];
    moneytextField.keyboardType = UIKeyboardTypeDecimalPad;
    moneytextField.delegate = self;
    [moneytextField addTarget:self action:@selector(textchange:) forControlEvents:UIControlEventEditingChanged];
    [self.moneyView addSubview:moneytextField];
    self.money = moneytextField;
    [moneytextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label1.mas_right).offset(0);
        make.right.equalTo(label2.mas_left).offset(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    UILabel *notictLabel = [[UILabel alloc]init];
    notictLabel.text = @"当前为拼手气红包";
    notictLabel.font = [UIFont systemFontOfSize:10];
    notictLabel.textAlignment = NSTextAlignmentCenter;
    [self.backScrollView addSubview:notictLabel];
    [notictLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.moneyView.mas_bottom).offset(0);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(85);
    }];
    self.noticeLabel = notictLabel;
    UIButton *changeButton = [[UIButton alloc]init];
    [changeButton addTarget:self action:@selector(changeMoneyType:) forControlEvents:UIControlEventTouchUpInside];
    changeButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [changeButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [changeButton setTitle:@"更改为为普通红包" forState:UIControlStateNormal];
    [self.backScrollView addSubview:changeButton];
    [changeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(notictLabel.mas_right).offset(0);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.top.equalTo(weakself.moneyView.mas_bottom).offset(0);
    }];
    
    self.contentview = [[EFengComposeTextView alloc]init];
    _contentview.layer.borderWidth = 0.5f;
    _contentview.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _contentview.layer.cornerRadius = 2.5f;
    _contentview.eFengComposeTextViewDelegate = self;
    _contentview.textFont = [UIFont systemFontOfSize:FontSize];
    [_contentview setMAXCHARACTERCOUNT:30];
    [_contentview setPlaceHoldText:@"送你一个红包"];
    _contentview.backgroundColor = WhiteColor;
    _contentview.userInteractionEnabled = YES;
    [self.backScrollView addSubview:_contentview];
    [_contentview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.moneyView.mas_bottom).offset(30);
        make.height.mas_equalTo(60);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(weakself.view.width - 40);
    }];
    self.resultLabel = [[UILabel alloc]init];
    self.resultLabel.text = @"¥0.00";
    self.resultLabel.textAlignment = NSTextAlignmentCenter;
    self.resultLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [self.backScrollView addSubview:_resultLabel];
    [_resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentview.mas_bottom).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
        make.centerX.mas_equalTo(weakself.view);
    }];
    self.sendButton = [[UIButton alloc]init];
    [self.sendButton addTarget:self action:@selector(sendMoney) forControlEvents:UIControlEventTouchUpInside];
    [self.sendButton setBackgroundImage:[UIImage imageNamed:@"r-send-button-moren"] forState:UIControlStateDisabled];
    [self.sendButton setBackgroundImage:[UIImage imageNamed:@"r-send-button-click"] forState:UIControlStateNormal];
    [self.sendButton setTitle:@"发红包" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:FontSize];
    self.sendButton.enabled = NO;
    [self.backScrollView addSubview:self.sendButton];
    [_sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.resultLabel.mas_bottom).offset(20);
        make.left.mas_equalTo(40);
        make.width.mas_equalTo(weakself.view.width - 80);
        make.height.mas_equalTo(40);
    }];

}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
    [futureString  insertString:string atIndex:range.location];
    
    NSInteger flag=0;
    const NSInteger limited = 2;
    for (NSInteger i = futureString.length-1; i>=0; i--) {
        
        if ([futureString characterAtIndex:i] == '.') {
            
            if (flag > limited) {
                return NO;
            }
            
            break;
        }
        flag++;
    }
    
    return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
       if (self.contact&&self.group==nil) {
        [self setupPersonMoneyView];
           self.type = PERSON;

    }
    else
    {
        [self setupGroupMoneyView];
        self.type = PINSHOUQI;
        
    }
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";

    [[CXmppRequestMgr shareInstance] getPersonbalanceWithUserId:self.currentUser.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [hud hide:YES];
                self.sendButton.enabled = YES;
                self.balanceLable.text =[@"¥" stringByAppendingString:responseObject[@"money"]];
            });
        }
        
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [hud hide:YES];
        [MBProgressHUD showError:@"获取余额失败"];
        self.sendButton.enabled = NO;
    }];

}
#pragma mark textField KVO
-(void)textchange:(UITextField*)textField
{
    if (textField.text.integerValue>200) {
        [MBProgressHUD showError:@"超出红包限额"];
        self.resultLabel.text = @"¥0.00";
        self.sendButton.enabled = NO;
        return;
    }
    if (self.group) {
        if (self.type == PINSHOUQI) {
            self.resultLabel.text = [@"¥" stringByAppendingString:[NSString stringWithFormat:@"%.2f",[textField.text doubleValue]]];
            
        }
        else
        {
            self.resultLabel.text = [@"¥" stringByAppendingString:[NSString stringWithFormat:@"%.2f",[textField.text doubleValue] *[self.count.text integerValue]]];
        }
    }
    else
    {
        self.resultLabel.text = [@"¥" stringByAppendingString:[NSString stringWithFormat:@"%.2f",[textField.text doubleValue]]];
    }
   
       if (textField.text.length == 0) {
        self.resultLabel.text = @"¥0.00";
        self.sendButton.enabled = NO;
        return;
    }
    self.sendButton.enabled = YES;
}
-(void)checkNumber:(UITextField*)textField
{
    if (textField.text.length == 0) {
        self.sendButton.enabled = NO;
        return;
    }
}
/*发送红包消息*/
-(void)sendMoney
{
    
    [self.view endEditing:YES];
    if (![UIUtil isNetworkReachable]) {
        [MBProgressHUD showError:@"网络错误"];
        return;
    }
    float blance = [self.balanceLable.text substringFromIndex:1].doubleValue;

    if (self.group) {
        if (self.type == NORMAL) {
            if ([self.money.text doubleValue]<0.01) {
                JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"错误金额" message:@"平均金额不能小于1分"];
                [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
                    
                }];
                [alert show];
                return;
            }
            
            if (self.money.text.floatValue * self.count.text.integerValue > blance) {
                JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"余额不足" message:@"您的余额不足,请到我的钱包中充值"];
                [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
                    
                }];
                
                [alert show];
                return;
            }

            
        }
        else
        {

            if (self.money.text.floatValue > blance) {
                JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"余额不足" message:@"您的余额不足,请到我的钱包中充值"];
                [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
                    
                }];
                
                [alert show];
                return;
            }
            if (self.money.text.floatValue / self.count.text.integerValue <0.01) {
                JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"错误金额" message:@"平均金额不能小于0.01"];
                [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
                    
                }];
                
                [alert show];
                return;
            }

        }

    }
  else
  {
      if (self.money.text.doubleValue<0.01) {
          JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"错误金额" message:@"金额不能小于0.1元"];
          [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
              
          }];
          [alert show];
          return;
      }
  }
    LAContext *context = [[LAContext alloc]init];
    if ([USER_DEFAULT boolForKey:self.currentUser.UserName]) {
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
            [self checkFinger];
        }
        else
        {
            self.passwordView=[[PopPasswordView alloc]initViewwithtype:@"open" andMoney:self.money.text];
            self.passwordView.delegate=self;
            self.passwordView.type=@"open";
            [[UIApplication sharedApplication].keyWindow addSubview:self.passwordView];
        }
   }
    else
    {
    self.passwordView=[[PopPasswordView alloc]initViewwithtype:@"open" andMoney:self.money.text];
    self.passwordView.delegate=self;
    self.passwordView.type=@"open";
    [[UIApplication sharedApplication].keyWindow addSubview:self.passwordView];
    }
}
-(void)checkFinger
{
    __weak typeof(self) weakSelf = self;
    NSString *str;
    if (self.contentview.text.length>0) {
        str = self.contentview.text;
    }
    else
    {
        str = @"送你一个红包";
    }

    NSString *money = [self.resultLabel.text substringFromIndex:1];
    LAContext* context = [[LAContext alloc] init];
    context.localizedFallbackTitle = @"";
    NSString* result = @"请验证您的指纹";
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
        if (success) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.labelText = @"发送中...";
            NSInteger count;
            if (self.count.text.length>0) {
                count = [self.count.text integerValue];
            }
            else
            {
                count = 1;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [[CXmppRequestMgr shareInstance] sendRedPacketMessageWithMessage:str messageId:nil curUser:self.currentUser member:self.contact group:self.group redPacketType:self.type money:[money doubleValue] number:count successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                    if ([responseObject[@"result"] isEqualToString:@"success"]){
                        [hud hide:YES];
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];

                        }];
                    }
                    else
                    {
                        [hud hide:YES];
                        [MBProgressHUD showError:@"发送失败请重试"];
                        
                    }
                } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                    [hud hide:YES];
                    [MBProgressHUD showError:@"发送失败请重试"];
                }];
                
            });
        }
        else
        {
            switch (error.code) {
                case LAErrorSystemCancel:
                {
                    NSLog(@"Authentication was cancelled by the system");
                    //切换到其他APP，系统取消验证Touch ID
                    break;
                }
                case LAErrorUserCancel:
                {
                    NSLog(@"Authentication was cancelled by the user");
                    //用户取消验证Touch ID
                    break;
                }
                case LAErrorUserFallback:
                {
                    NSLog(@"User selected to enter custom password");
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        //用户选择输入密码，切换主线程处理
                    }];
                    break;
                }
                default:
                {
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        //其他情况，切换主线程处理
                    }];
                    break;
                }
            }
        }
    }];

}
-(void)useStoreCode:(NSString *)code
{
    __weak typeof(self) weakSelf = self;
    [self.passwordView removeFromSuperview];
    NSString *str;
    if (self.contentview.text.length>0) {
        str = self.contentview.text;
    }
    else
    {
        str = @"送你一个红包";
    }
    /*发网络请求 验证密码 如果成功*/
    /*失败就提示错误*/
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"发送中...";
    NSString *money = [self.resultLabel.text substringFromIndex:1];
    [[CXmppRequestMgr shareInstance] verificationPayPasswordWithUserId:self.currentUser.UserID AndPassWord:code successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            NSInteger count;
            if (self.count.text.length>0) {
                count = [self.count.text integerValue];
            }
            else
            {
                count = 1;
            }
         dispatch_async(dispatch_get_main_queue(), ^{
             [[CXmppRequestMgr shareInstance] sendRedPacketMessageWithMessage:str messageId:nil curUser:self.currentUser member:self.contact group:self.group redPacketType:self.type money:[money doubleValue] number:count successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                 if ([responseObject[@"result"] isEqualToString:@"success"]){
                     [hud hide:YES];
                     dispatch_async(dispatch_get_main_queue(), ^{
                         
                         [weakSelf.navigationController popViewControllerAnimated:YES];
                     });
                 }
                 else
                 {
                     [hud hide:YES];
                     [MBProgressHUD showError:@"发送失败请重试"];
                     
                 }
             } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                 [hud hide:YES];
                 [MBProgressHUD showError:@"发送失败请重试"];
             }];

         });
            
        }
        else
        {
            [hud hide:YES];
            [MBProgressHUD showError:@"密码错误请重试"];
            [self sendMoney];
        }
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [hud hide:YES];
        [MBProgressHUD showError:@"密码错误请重试"];
        [self sendMoney];
    }];
 
    
   
    
}
-(void)disAction
{
    [self.passwordView removeFromSuperview];
    
}

-(void)changeMoneyType:(UIButton*)sender
{
    self.count.text = @"";
    self.money.text = @"";
    self.resultLabel.text = @"";
    if (self.type == PINSHOUQI) {
        self.type = NORMAL;
        [sender setTitle:@"更改为拼手气红包" forState:UIControlStateNormal];
        self.noticeLabel.text = @"当前为普通红包";
        self.money.placeholder = @"请输入单个红包金额";
    }
    else
    {
        self.type = PINSHOUQI;
        [sender setTitle:@"更改为普通红包" forState:UIControlStateNormal];
        self.noticeLabel.text = @"当前为拼手气红包";
        self.money.placeholder = @"请输入红包金额";

    }
}

- (void)loginkeyboardWillShow:(NSNotification *)notif {
    
    
    CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];

    if (rect.origin.y >= CGRectGetMaxY(_sendButton.frame)) {
        
    } else {
        [_backScrollView setContentOffset:CGPointMake(0, CGRectGetMaxY(_sendButton.frame) - rect.origin.y) animated:NO];
    }
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notif {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];

    [_backScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    [UIView commitAnimations];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
